#include "hplcpumpmotorcurrentcalibrationpage.h"
#include "modulemanager.h"
#include "modulehplcpump.h"
#include "modulehplcpumpparamiddefine.h"
#include "modulehplccmdid.h"
#include "tableviewex.h"
#include <QDebug>
#include <QCloseEvent>

HplcPumpMotorCurrentCalibrationPage::HplcPumpMotorCurrentCalibrationPage(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("Hplc Pump Pressure Calibrate Page"));
    setAttribute(Qt::WA_DeleteOnClose);

    m_tableView = new TableViewEx(this);
    m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    m_tableView->setSortingEnabled(true);
    QStringList headers;
    headers << tr("rpm") << tr("current(1~31)");
    m_tableView->setHeaders(headers);

    ModuleHplcPump *mod = static_cast<ModuleHplcPump*>(moduleManager->find(ModuleID_HPLC_PumpA));
    if (mod == nullptr)
        return;
    connect(mod, &ModuleHplcPump::parseCompleted,
            this, &HplcPumpMotorCurrentCalibrationPage::onParseCompleted, Qt::QueuedConnection);

    QVariant var;
    QList<hplc::PumpStepMotorCurrentCalibData> list;
    mod->getParam(ModuleHplcPumpParamID_StepMotorCurrentCalibData, var);
    list.clear();
    list = var.value<QList<hplc::PumpStepMotorCurrentCalibData>>();
    onSetData(list);

    // 进入步进电机电流校正模式
    m_pbWorkMode = new QPushButton();
    m_pbWorkMode->setText(tr("Enter"));
    connect(m_pbWorkMode, &QPushButton::clicked, this, [=](){
        QVariant var;
        if (QString::compare(m_pbWorkMode->text(), tr("Enter")) == 0)
        {
            var.setValue(true);
            mod->setProtocolCmd(hplc::PumpSystemCmdID_SetWorkModeMotorCurrentCalib, var);

            m_pbWorkMode->setText(tr("Exit"));
        }
        else
        {
            var.setValue(false);
            mod->setProtocolCmd(hplc::PumpSystemCmdID_SetWorkModeMotorCurrentCalib, var);

            m_pbWorkMode->setText(tr("Enter"));
        }
    });

    // 保存到板
    m_pbSet = new QPushButton();
    m_pbSet->setText(tr("Set"));
    connect(m_pbSet, &QPushButton::clicked, this, [=](){
        QList<hplc::PumpStepMotorCurrentCalibData> list = getData();
        // 保存到模块
        QVariant var;
        var.setValue(list);
        mod->setParam(ModuleHplcPumpParamID_StepMotorCurrentCalibData, var);

        mod->setProtocolCmd(hplc::PumpSystemCmdID_SetMotorCurrentCalibData, var);
    });
    m_pbGet = new QPushButton();
    m_pbGet->setText(tr("Get"));
    connect(m_pbGet, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::PumpSystemCmdID_GetMotorCurrentCalibData, var);
    });

    // 读板数据
    QVBoxLayout *vblSetGet = new QVBoxLayout();
    vblSetGet->addWidget(m_pbWorkMode);
    vblSetGet->addWidget(m_pbSet);
    vblSetGet->addWidget(m_pbGet);

    /*******************************************************
     ********************* 零点电压 *************************
     *******************************************************/
    m_lbIRun = new QLabel(tr("IRun"));
    m_leIRun = new QLineEdit();
    m_pbSetIRun = new QPushButton(tr("Set"));
    connect(m_pbSetIRun, &QPushButton::clicked, this, [=](){
        QVariant var;
        var.setValue(m_leIRun->text().toUInt());
        mod->setProtocolCmd(hplc::PumpSystemCmdID_SetStepMotorIRun, var);
    });
    m_pbGetIRun = new QPushButton(tr("Get"));
    connect(m_pbGetIRun, &QPushButton::clicked, this, [=](){
        QVariant var;
        mod->getProtocolCmd(hplc::PumpSystemCmdID_GetStepMotorIRun, var);
    });
    m_hblIRun = new QHBoxLayout();
    m_hblIRun->addWidget(m_lbIRun);
    m_hblIRun->addWidget(m_leIRun);
    m_hblIRun->addWidget(m_pbSetIRun);
    m_hblIRun->addWidget(m_pbGetIRun);

    QHBoxLayout *hbl = new QHBoxLayout();
    hbl->addWidget(m_tableView, 5);
    hbl->addLayout(vblSetGet, 1);
    hbl->addLayout(m_hblIRun, 1);
    setLayout(hbl);
}

HplcPumpMotorCurrentCalibrationPage::~HplcPumpMotorCurrentCalibrationPage()
{

}

QList<hplc::PumpStepMotorCurrentCalibData> HplcPumpMotorCurrentCalibrationPage::getData()
{
    QStandardItemModel &model = m_tableView->getModel();
    QList<hplc::PumpStepMotorCurrentCalibData> list;
    hplc::PumpStepMotorCurrentCalibData data;
    for (int i = 0; i < model.rowCount(); i++)
    {
        for (int j = 0; j < model.columnCount(); j++)
        {
            QVariant var = model.data(model.index(i, j), Qt::DisplayRole);

            if (j == 0)
                data.rpm = var.toDouble();
            else if (j == 1)
                data.current = var.toUInt();;
        }
        list << data;
    }
    foreach(auto var, list)
    {
        qDebug() << var.rpm << var.current;
    }
    return list;
}

void HplcPumpMotorCurrentCalibrationPage::onSetData(const QList<hplc::PumpStepMotorCurrentCalibData> &list)
{
    QStandardItemModel &model = m_tableView->getModel();
    model.removeRows(0, model.rowCount());
    QList<QStandardItem*> items;
    QStandardItem *itemRpm, *itemRate;
    foreach (auto var, list)
    {
        itemRpm = new QStandardItem();
        itemRpm->setData(var.rpm, Qt::DisplayRole);
        itemRate = new QStandardItem();
        itemRate->setData(var.current, Qt::DisplayRole);
        items.clear();
        items << itemRpm << itemRate;
        model.appendRow(items);
    }
}

void HplcPumpMotorCurrentCalibrationPage::closeEvent(QCloseEvent *event)
{
    disconnect();
    event->accept();
}

void HplcPumpMotorCurrentCalibrationPage::onParseCompleted(int id, const QVariant &var)
{
    switch (id)
    {
        case hplc::PumpSystemCmdID_SetMotorCurrentCalibData:
        {
            QList<hplc::PumpStepMotorCurrentCalibData> list;
            list = var.value<QList<hplc::PumpStepMotorCurrentCalibData>>();
            onSetData(list);
        }
        break;
        case hplc::PumpSystemCmdID_SetStepMotorIRun:
        {
            quint8 current = var.toUInt();
            m_leIRun->setText(QString::number(current));
        }
        break;
        default:
        break;
    }
}
